package edu.upenn.seas.peerreview

import scala.collection.mutable

/**
 * Implementation of a Debug object.
 */
object DebugImpl {
  private val macros = mutable.Map[String, Set[String]]()    
  
  /**
   * Logs a message through the Debug utility.
   * @param src the source tag
   * @param msg the contents
   */
  def apply(src: String)(msg: String) : Unit = {
    import Debug.debugLevel
    
    if (debugLevel == Nil) return   // No flags set.
      
    // All macros (flags) which would allow this msg
    val valids = macros.filterKeys(flag => macros(flag).exists(_ == src)).keySet
    
    // If in a macro which was specified or if hand-specified
    if (debugLevel.exists(flag => flag == "all" 
        || flag == src 
        || valids.exists(_ == flag) ))
      println("[%s] %s".format(src, msg))
  }
  
  /**
   * Define what message sources a particular flag wants to subscribe to.
   * @param flag the flag name (e.g. transport, ui, sm); "all" is reserved
   * @param srcs a list of src tags
   */
  def watch(flag: String)(srcs: String*) : Unit = {
    if (flag == "all") return; // Not permitted to change 'all' subscriptions
    macros.put(flag, Set() ++ (srcs.toArray))
  }
}
